package com.example.mydemo;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mydemo.bean.Item;

import java.util.Date;
import java.util.List;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class ShowListActivity extends AppCompatActivity {

    private ListView listView;
    private MyAdapter adapter;
    private List<Item> itemList;

    private void getTodayMessagesAndCalls() {
        ContentResolver cr = getContentResolver();

        // 获取今天日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String todayDate = sdf.format(new Date());

        // 查询短信
        Uri smsUri = Uri.parse("content://sms/inbox");
        Cursor smsCursor = cr.query(smsUri, null, null, null, null);
        if (smsCursor != null && smsCursor.moveToFirst()) {
            do {
                long dateMillis = smsCursor.getLong(smsCursor.getColumnIndexOrThrow("date"));
                String date = sdf.format(new Date(dateMillis));
                if (todayDate.equals(date)) {
                    String time = new SimpleDateFormat("HH:mm").format(new Date(dateMillis));
                    String number = smsCursor.getString(smsCursor.getColumnIndexOrThrow("address"));
                    String body = smsCursor.getString(smsCursor.getColumnIndexOrThrow("body"));
                    itemList.add(new Item(time, number, "目标手机收到了一条短信: " + body));
                }
            } while (smsCursor.moveToNext());
            smsCursor.close();
        }
        // 查询通话记录
        Uri callLogUri = Uri.parse("content://call_log/calls");
        Cursor callCursor = cr.query(callLogUri, null, null, null, null);
        if (callCursor != null && callCursor.moveToFirst()) {
            do {
                long dateMillis = callCursor.getLong(callCursor.getColumnIndexOrThrow("date"));
                String date = sdf.format(new Date(dateMillis));
                if (todayDate.equals(date)) {
                    String time = new SimpleDateFormat("HH:mm").format(new Date(dateMillis));
                    String number = callCursor.getString(callCursor.getColumnIndexOrThrow("number"));
                    int type = callCursor.getInt(callCursor.getColumnIndexOrThrow("type"));
                    if (type == CallLog.Calls.MISSED_TYPE) {
                        Calendar cal = Calendar.getInstance();
                        cal.setTimeInMillis(dateMillis);
                        String missedCallInfo = "目标手机未接来电:" + (cal.get(Calendar.MONTH) + 1) + "月" +
                                cal.get(Calendar.DAY_OF_MONTH) + "号" +
                                cal.get(Calendar.HOUR_OF_DAY) + "时" +
                                cal.get(Calendar.MINUTE) + "分";
                        itemList.add(new Item(time, number, missedCallInfo));
                    }
                }
            } while (callCursor.moveToNext());
            callCursor.close();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        findViewById(R.id.back).setOnClickListener(v -> finish());//点击返回
        listView = findViewById(R.id.listView);
        // 准备数据
        itemList = new ArrayList<>();
        //读取并插入数据
        getTodayMessagesAndCalls();
        // 创建适配器
        adapter = new MyAdapter(this, itemList);
        listView.setAdapter(adapter);
        // 设置点击事件
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Item item = itemList.get(position);
            //后续可以弹窗展现全部内容
        });
    }
}

